package com.learn.springcloud.eurekahaconsumer.controller;

import com.learn.springcloud.eurekahaconsumer.bean.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Time : 2018/7/23 19:40
 * @Version : 1.0
 * @Author : hongyan.li@hand-china.com
 * Copyright @ 2000 Shanghai Hand Co. Ltd.
 * All right reserved.
 */
@RestController
public class PostConsumer {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForEntity/example1")
    public Date postForEntityExample1(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForEntity1/service";
        ResponseEntity<Users> responseEntity = restTemplate.postForEntity(url, users, Users.class);
        return responseEntity.getBody().getBirthday();
    }


    /**
     * postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForEntity/example11")
    public Date postForEntityExample11(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForEntity2/service?anything={anything}";
        ResponseEntity<Users> responseEntity = restTemplate.postForEntity(url, users, Users.class,"hello world");
        return responseEntity.getBody().getBirthday();
    }


    /**
     * postForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForEntity/example2")
    public Date postForEntityExample2(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForEntity1/service";
        ResponseEntity<Users> responseEntity = restTemplate.postForEntity(url, users, Users.class, new HashMap<>());
        return responseEntity.getBody().getBirthday();
    }


    /**
     * postForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForEntity/example22")
    public Date postForEntityExample22(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForEntity2/service?anything={anything}";
        Map<String,String> map = new HashMap<>();
        map.put("anything", "hello world");
        ResponseEntity<Users> responseEntity = restTemplate.postForEntity(url, users, Users.class,map);
        return responseEntity.getBody().getBirthday();
    }


    /**
     * postForObject(URI url, Object request, Class<T> responseType)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForEntity/example3")
    public Date postForEntityExample3(@RequestBody Users users){
        String result = "";
        UriComponents uriComponents = UriComponentsBuilder
                .fromUriString("http://ribbon-resttemplate-example-provider/provider/postForEntity1/service")
                .build()
                .expand("")
                .encode();
        URI url= uriComponents.toUri();
        ResponseEntity<Users> responseEntity = restTemplate.postForEntity(url,users,Users.class);
        return responseEntity.getBody().getBirthday();
    }


    /**
     * postForObject(URI url, Object request, Class<T> responseType)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForEntity/example33")
    public Date postForEntityExample33(@RequestBody Users users){
        String result = "";
        UriComponents uriComponents = UriComponentsBuilder
                .fromUriString("http://ribbon-resttemplate-example-provider/provider/postForEntity1/service?anything={anything}")
                .build()
                .expand("hello world")
                .encode();
        URI url= uriComponents.toUri();
        ResponseEntity<Users> responseEntity = restTemplate.postForEntity(url,users,Users.class);
        return responseEntity.getBody().getBirthday();
    }


    /**
     * postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForObject/example1")
    public Date postForObjectExample1(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForObject1/service";
        Date responseEntity = restTemplate.postForObject(url, users, Date.class);
        return responseEntity;
    }


    /**
     * postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForObject/example11")
    public Date postForObjectExample11(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForObject2/service?anything={anything}";
        Date responseEntity = restTemplate.postForObject(url, users, Date.class,"hello world");
        return responseEntity;
    }

    /**
     * postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForObject/example2")
    public Date postForObjectExample2(@RequestBody Users users){
        String url = "http://ribbon-resttemplate-example-provider/provider/postForObject1/service";
        Date responseEntity = restTemplate.postForObject(url,users,Date.class,new HashMap<>());
        return responseEntity;
    }


    /**
     * postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForObject/example22")
    public Date postForObjectExample22(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForObject2/service?anything={anything}";
        Map<String,String> map = new HashMap<>();
        map.put("anything","hello world");
        Date responseEntity = restTemplate.postForObject(url, users, Date.class,map);
        return responseEntity;
    }

    /**
     * postForObject(URI url, Object request, Class<T> responseType)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForObject/example3")
    public Date postForObjectExample3(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForObject1/service";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build().expand("").encode();
        URI uri = uriComponents.toUri();
        Date responseEntity = restTemplate.postForObject(uri,users,Date.class);
        return responseEntity;
    }


    /**
     * postForObject(URI url, Object request, Class<T> responseType)
     * @param users
     * @return
     */
    @PostMapping(value = "/consumer/postForObject/example33")
    public Date postForObjectExample33(@RequestBody Users users){
        String result = "";
        String url = "http://ribbon-resttemplate-example-provider/provider/postForObject2/service?anything={anything}";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build().expand("hello world").encode();
        URI uri = uriComponents.toUri();
        Date responseEntity = restTemplate.postForObject(uri,users,Date.class);
        return responseEntity;
    }

}
